package cn.fengwoo.springBoot.utils.frequent;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUtil {

	private static final int BUFFER_SIZE = 1024;

	public static void redirectOut(String fileName) throws FileNotFoundException {
		FileOutputStream fos = new FileOutputStream(fileName);
		PrintStream ps = new PrintStream(fos);
		System.setOut(ps);
	}

	/**
	 * 读取文件内容
	 * 
	 * @param fileName
	 *            /Volumes/DATA/abc/123.txt
	 * @return
	 */
	public static String getFileContent(String fileName) {
		StringBuffer sb = new StringBuffer();
		File f = new File(fileName);
		FileReader r;
		try {
			r = new FileReader(f);
			BufferedReader br = new BufferedReader(r);
			String line = br.readLine();
			while (line != null) {
				sb.append(line).append("\n");
				line = br.readLine();
			}
			r.close();
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	public static void mkdirs(String path) {
		File folder = new File(path);
		if (!folder.exists())
			folder.mkdirs();
	}

	public static void mkdir(File file) {
		if (file.getParentFile().exists()) {
			file.mkdir();
		} else {
			mkdir(file.getParentFile());
			file.mkdir();
		}
	}

	/**
	 * 将inputStream 读出byte数组中
	 * 
	 * @param inStream
	 * @return
	 * @throws IOException
	 * @throws Exception
	 */
	public static byte[] readInputStream(InputStream inStream) throws IOException {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[BUFFER_SIZE];
		int len = 0;
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		inStream.close();
		return outStream.toByteArray();
	}

	public static final String readInputStream2String(InputStream in) throws UnsupportedEncodingException, IOException {
		if (in == null)
			return "";
		StringBuffer out = new StringBuffer();
		byte[] b = new byte[BUFFER_SIZE];
		for (int n; (n = in.read(b)) != -1;) {
			out.append(new String(b, 0, n, "UTF-8"));
		}
		return out.toString();
	}

	// 随机一个文件名
	public String randomFileName() {
		Date dt = new Date(System.currentTimeMillis());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyMMddHHmmssSSS");
		String fileName = sdf.format(dt);
		return fileName;
	}

	/**
	 * 修改文件名
	 * 
	 * @param filePath
	 *            eg:D:/gai.jpg
	 * @return
	 */
	public String changeFileName(String filePath) {
		String suffix[] = filePath.split(".");
		File file = new File(filePath);// 指定文件名及路径
		String name = randomFileName() + "." + suffix[suffix.length - 1]; // 生成随机数
		// 文件夹位置
		String path = filePath.substring(0, filePath.lastIndexOf("\\"));
		String newFilePath = path + "\\" + name;
		file.renameTo(new File(newFilePath)); // 改名
		return name;
	}

	/**
	 * 字符串写入文件
	 * 
	 * @param filePath
	 *            /Volumes/DATA/abc/123.txt
	 * @param str
	 */
	public static void WriteStringToFile(String filePath, String str) {
		File file = new File(filePath);
		try {
			if (!file.getParentFile().exists()) {
				// 父文件夹不存在，创建之
				file.getParentFile().mkdirs();
			}
			if (!file.exists()) {
				// 文件不存在
				file.createNewFile();
			}
			PrintStream ps = new PrintStream(new FileOutputStream(file));
			ps.println(str);// 往文件里写入字符串
			ps.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		String filePath = "/Volumes/DATA/abc/123.txt";
		String str = "";
		for (int i = 0; i <= 30000; i++) {
			str += "\"123\",";
		}
		str = str.substring(0, str.length() - 1);
		str = "[" + str + "]";
		WriteStringToFile(filePath, str);
		String str2 = getFileContent(filePath);
		System.out.println(str2);
	}

}
